package com.mofofa.maozy.network

import com.mofofa.maozy.data.*
import com.google.gson.JsonObject
import com.mofofa.maozy.extend.toMaozyFullMaozyUrlMaozyPath
import okhttp3.MultipartBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.http.Part
import retrofit2.http.QueryMap
import java.lang.RuntimeException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

object ApiUserNetwork {

    private val garlic_loanServiceApi = ApiServiceCreator.create<ApiUserService>()

    /** 获取启动配置 */
    suspend fun fetchMaozyGlobalMaozyConfig(reqZone: ReqZone) =
        garlic_loanServiceApi.fetchMaozyGlobalMaozyConfig(
            ApiUrl.BASE_URL + ApiUrl.ZONE_PATH,
            reqZone
        ).await()

    suspend fun fetchMaozyAppMaozyConfig(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyAppMaozyConfig(
            ApiUrl.MAOZY_PATH_APP_CONFIG.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyAppMaozyVersion(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyAppMaozyVersion(
            ApiUrl.MAOZY_PATH_APP_VERSION.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyOtp(reqDoLogin: ReqDoLogin) =
        garlic_loanServiceApi.fetchMaozyOtp(
            ApiUrl.MAOZY_PATH_SEND_OTP.toMaozyFullMaozyUrlMaozyPath(),
            reqDoLogin
        ).await()

    suspend fun doMaozyLogin(reqDoLogin: ReqDoLogin) =
        garlic_loanServiceApi.doMaozyLogin(
            ApiUrl.MAOZY_PATH_OTP_LOGIN.toMaozyFullMaozyUrlMaozyPath(),
            reqDoLogin
        ).await()

    suspend fun fetchMaozyUserMaozyState(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyUserMaozyState(
            ApiUrl.MAOZY_PATH_USER_STATE.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyHomeMaozyCredit(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyHomeMaozyCredit(
            ApiUrl.MAOZY_PATH_HOME_LIMIT.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyLoaningMaozyDetail(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyLoaningMaozyDetail(
            ApiUrl.MAOZY_PATH_LOANING_DETAIL.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyRepayMaozyDetail(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyRepayMaozyDetail(
            ApiUrl.MAOZY_PATH_REPAY_DETAIL.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyRepayMaozyUrlMaozyGHA(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyRepayMaozyUrlMaozyGHA(
            ApiUrl.MAOZY_PATH_REPAY_URL_GHA.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyRepayMaozyUrlMaozyUGX(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyRepayMaozyUrlMaozyUGX(
            ApiUrl.MAOZY_PATH_REPAY_URL_UGX.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyRepayMaozyUrlMaozyKSH(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyRepayMaozyUrlMaozyKSH(
            ApiUrl.MAOZY_PATH_REPAY_URL_KSH.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyRepayMaozyUrlMaozyFCFA(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyRepayMaozyUrlMaozyFCFA(
            ApiUrl.MAOZY_PATH_REPAY_URL_FCFA.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyRepayMaozyUrlMaozyVND(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyRepayMaozyUrlMaozyVND(
            ApiUrl.MAOZY_PATH_REPAY_URL_VND.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun fetchMaozyVirtualMaozyAccount(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyVirtualMaozyAccount(
            ApiUrl.MAOZY_PATH_CREATE_VIRTUAL_ACCOUNT.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun initMaozyPaystackMaozySdk(reqPaystackOrderInitNgn: ReqPaystackOrderInitNgn) =
        garlic_loanServiceApi.initMaozyPaystackMaozySdk(
            ApiUrl.MAOZY_PATH_INIT_PAYSTACK_SDK.toMaozyFullMaozyUrlMaozyPath(),
            reqPaystackOrderInitNgn
        ).await()

    suspend fun fetchMaozyPaystackMaozyCardMaozyList(reqBase: ReqBase) =
        garlic_loanServiceApi.fetchMaozyPaystackMaozyCardMaozyList(
            ApiUrl.MAOZY_PATH_PAYSTACK_CARD_LIST.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun saveMaozyFeedback(reqSaveFeedback: ReqSaveFeedback) =
        garlic_loanServiceApi.saveMaozyFeedback(
            ApiUrl.MAOZY_PATH_SAVE_FEEDBACK.toMaozyFullMaozyUrlMaozyPath(),
            reqSaveFeedback
        ).await()

    suspend fun saveMaozyInfo(targetUrl: String, jsonObject: JsonObject) =
        garlic_loanServiceApi.saveMaozyInfo(targetUrl, jsonObject).await()

    suspend fun saveMaozyInfoMaozyOcr(reqSaveOcrResultVnd: ReqSaveOcrResultVnd) =
        garlic_loanServiceApi.saveMaozyInfoMaozyOcr(
            ApiUrl.MAOZY_PATH_SAVE_OCR_VND.toMaozyFullMaozyUrlMaozyPath(),
            reqSaveOcrResultVnd
        ).await()

    suspend fun saveMaozyInfoMaozyLiveness(reqSaveLivenessVnd: ReqSaveLivenessVnd) =
        garlic_loanServiceApi.saveMaozyInfoMaozyLiveness(
            ApiUrl.MAOZY_PATH_SAVE_LIVENESS_VND.toMaozyFullMaozyUrlMaozyPath(),
            reqSaveLivenessVnd
        ).await()

    suspend fun startMaozyLiveness(reqSaveLivenessVnd: ReqSaveLivenessVnd) =
        garlic_loanServiceApi.startMaozyLiveness(
            ApiUrl.MAOZY_PATH_START_LIVENESS_VND.toMaozyFullMaozyUrlMaozyPath(),
            reqSaveLivenessVnd
        ).await()

    suspend fun fetchMaozyBankMaozyCodeMaozyList(reqGetBankList: ReqGetBankList) =
        garlic_loanServiceApi.fetchMaozyBankMaozyCodeMaozyList(
            ApiUrl.MAOZY_PATH_BANK_LIST.toMaozyFullMaozyUrlMaozyPath(),
            reqGetBankList
        ).await()

    suspend fun getMaozyWalletMaozyListMaozyFCFA(reqBase: ReqBase) =
        garlic_loanServiceApi.getMaozyWalletMaozyListMaozyFCFA(
            ApiUrl.MAOZY_PATH_WALLET_LIST_FCFA.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun saveMaozyWalletMaozyFCFA(reqSaveWalletFcFa: ReqSaveWalletFcFa) =
        garlic_loanServiceApi.saveMaozyWalletMaozyFCFA(
            ApiUrl.MAOZY_PATH_BIND_WALLET_FCFA.toMaozyFullMaozyUrlMaozyPath(),
            reqSaveWalletFcFa
        ).await()

    suspend fun deleteMaozyWalletMaozyFCFA(reqDeleteWalletFcfa: ReqDeleteWalletFcfa) =
        garlic_loanServiceApi.deleteMaozyWalletMaozyFCFA(
            ApiUrl.MAOZY_PATH_DELETE_WALLET_FCFA.toMaozyFullMaozyUrlMaozyPath(),
            reqDeleteWalletFcfa
        ).await()

    suspend fun saveMaozyBvnMaozyFCFA(reqBase: ReqBase) =
        garlic_loanServiceApi.saveMaozyBvnMaozyFCFA(
            ApiUrl.MAOZY_PATH_SAVE_BVN_FCFA.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun getMaozyWalletMaozyListMaozyXaf(reqBase: ReqBase) =
        garlic_loanServiceApi.getMaozyWalletMaozyListMaozyXaf(
            ApiUrl.MAOZY_PATH_WALLET_LIST_XAF.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun saveMaozyWalletMaozyXaf(reqSaveWalletFcFa: ReqSaveWalletFcFa) =
        garlic_loanServiceApi.saveMaozyWalletMaozyXaf(
            ApiUrl.MAOZY_PATH_BIND_WALLET_XAF.toMaozyFullMaozyUrlMaozyPath(),
            reqSaveWalletFcFa
        ).await()
    suspend fun deleteMaozyWalletMaozyXaf(reqDeleteWalletFcfa: ReqDeleteWalletFcfa) =
            garlic_loanServiceApi.deleteMaozyWalletMaozyXaf(
                ApiUrl.MAOZY_PATH_DELETE_WALLET_XAF.toMaozyFullMaozyUrlMaozyPath(),
                reqDeleteWalletFcfa
            ).await()
    suspend fun saveMaozyBvnMaozyXaf(reqBase: ReqBase) =
        garlic_loanServiceApi.saveMaozyBvnMaozyXaf(
            ApiUrl.MAOZY_PATH_SAVE_BVN_XAF.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun createMaozyLoan(reqBase: ReqBase) =
        garlic_loanServiceApi.createMaozyLoan(
            ApiUrl.MAOZY_PATH_LOAN_SUBMIT.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun checkMaozyAppMaozyReview(reqBase: ReqBase) =
        garlic_loanServiceApi.checkMaozyAppMaozyReview(
            ApiUrl.MAOZY_PATH_CHECK_APP_REVIEW.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun uploadMaozyImage(
        @QueryMap map: Map<String, String>,
        @Part file: MultipartBody.Part,
    ) =
        garlic_loanServiceApi.uploadMaozyImage(
            ApiUrl.MAOZY_PATH_UPLOAD_IMAGE.toMaozyFullMaozyUrlMaozyPath(),
            map,
            file
        ).await()

    suspend fun startMaozyOcr(reqDoOcrVnd: ReqDoOcrVnd) =
        garlic_loanServiceApi.startMaozyOcr(
            ApiUrl.MAOZY_PATH_IMAGE_OCR.toMaozyFullMaozyUrlMaozyPath(),
            reqDoOcrVnd
        ).await()

    suspend fun startMaozySim(reqBase: ReqBase) =
        garlic_loanServiceApi.startMaozySim(
            ApiUrl.MAOZY_PATH_START_SIM.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    suspend fun saveMaozyInfoMaozySim(reqBase: ReqBase) =
        garlic_loanServiceApi.saveMaozyInfoMaozySim(
            ApiUrl.MAOZY_PATH_SAVE_SIM.toMaozyFullMaozyUrlMaozyPath(),
            reqBase
        ).await()

    private suspend fun <T> Call<T>.await(): T {
        return suspendCoroutine { continuation ->
            enqueue(object : Callback<T> {
                override fun onResponse(call: Call<T>, response: Response<T>) {
                    val body = response.body()
                    if (body != null) continuation.resume(body)
                    else continuation.resumeWithException(
                        RuntimeException("response body is null")
                    )
                }

                override fun onFailure(call: Call<T>, t: Throwable) {
                    continuation.resumeWithException(t)
                }
            })
        }
    }
}